package com.example.lenovo.test01;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;

import java.util.Timer;
import java.util.TimerTask;

public class UC3 extends AppCompatActivity {

    private LinearLayout linearlayout;
    private Button btn_back3;
    private ImageView mimageview;

    private int i = 0;
    //定时器
    Timer timer = new Timer();

    private Handler handler = new Handler()
    {
        @Override
        public void handleMessage(Message msg)
        {

            Log.e("@@@", i + "");
            //index=msg.what;
            //实现轮播
            if (i > 6)
            {
                i = 0;
            }
            else
            {
                switch (i)
                {
                    case 1:
                        mimageview.setImageResource(R.drawable.image1);
                        break;
                    case 2:
                        mimageview.setImageResource(R.drawable.image2);
                        break;
                    case 3:
                        mimageview.setImageResource(R.drawable.image3);
                        break;
                    case 4:
                        mimageview.setImageResource(R.drawable.image4);
                        break;
                    case 5:
                        mimageview.setImageResource(R.drawable.image5);
                        break;
                    default:
                        break;
                }
                linearlayout.invalidate();
            }
            super.handleMessage(msg);
        }
    };

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_uc3);

        btn_back3=findViewById(R.id.btn_return3);
        btn_back3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent =new Intent(UC3.this,MainActivity.class);
                startActivity(intent);
            }
        });

        initView();
    }

    public void initView()
    {
        linearlayout = findViewById(R.id.ll_3);
        mimageview = findViewById(R.id.image);

        timer.scheduleAtFixedRate(new TimerTask()
        {
            @Override
            public void run()
            {
                // TODO Auto-generated method stub
                i++;
                Message mesasge = new Message();
                mesasge.what = i;
                handler.sendMessage(mesasge);
            }
        }, 0, 500);
    }

    @Override
    protected void onDestroy()
    {
        // TODO Auto-generated method stub
        timer.cancel();
        super.onDestroy();
    }
}
